package com.wjw.java.io.properties_;

import java.io.*;
import java.util.Properties;

/**
 * 1、利用System.getProperty()函数获取当前路径：
 * System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径
 */
public class Properties_1 {
    public static void main(String[] args) {
        readProperties();
        System.out.println("===============================================");
        //writeProperties();
    }

    /**
     * 读取Properties文件中的数据
     */
    public static void readProperties(){
        //Properties的数据结构为K V
        Properties properties = new Properties();
        String path = "C:\\Users\\wjw\\Desktop\\basic\\src\\main\\java\\com\\wjw\\java\\io\\properties_\\";
        try {
            //加载配置文件
            properties.load(new FileReader(path +"mysql.properties"));
            //控制台打印输出
            properties.list(System.out);
            //获取单个值
            String username = properties.getProperty("username");
            System.out.println("username:"+username);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改、写入数据到Properties文件中
     */
    public static void writeProperties(){
        Properties properties = new Properties();
        String path = "C:\\Users\\wjw\\Desktop\\basic\\src\\main\\java\\com\\wjw\\java\\io\\properties_\\";
        //该文件中没有的key,就是创建
        //该文件中有的key,就是修改
        properties.setProperty("user","吴佳威");//注意,保存中文时,保存的是unicode码值
        properties.setProperty("pass","123456");
        properties.setProperty("pass","666666");//修改
        try {
            //properties.store(new FileOutputStream(path +"mysql2.properties"),null);
            properties.store(new FileOutputStream(path +"mysql2.properties"),"这是注释");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
